package com.ssy.lingxi.member.merchant.handler.validator;

import com.ssy.lingxi.member.merchant.handler.annotation.MemberValidateAgreeAnnotation;
import com.ssy.lingxi.member.merchant.model.constant.MemberValidateAgreeEnum;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * 会员审批状态枚举校验注解实现类
 * @author 万宁
 * @version 2.0.0
 * @date 2020-07-29
 */
public class MemberValidateAgreeValidator implements ConstraintValidator<MemberValidateAgreeAnnotation, Integer> {
    private boolean required;
    @Override
    public void initialize(MemberValidateAgreeAnnotation constraintAnnotation) {
        required = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        if(!required) {
            return true;
        }

        if(value == null) {
            return false;
        }

        return Arrays.stream(MemberValidateAgreeEnum.values()).anyMatch(c -> c.getCode().equals(value));
    }
}
